<template>
  <y-dialog @open="open" @close="emit('close')" width="1000px" append-to-body>
    <y-code-editor ref="editorRef" v-model="form.script" language="python" />
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="AuScript">
const { proxy } = getCurrentInstance();

const emit = defineEmits(["submited", "close"]);

const editorRef = ref(null);

const data = reactive({
  form: {},
});

const { form } = toRefs(data);

const props = defineProps({
  id: { type: String, default: "" },
  getMethod: { type: Function, default: undefined },
  addMethod: { type: Function, default: undefined },
  updateMethod: { type: Function, default: undefined },
});

/** 打开对话框事件 */
function open() {
  props.getMethod(props.id).then((res) => {
    nextTick(() => {
      form.value = res.data;
    });
  });
}

/** 提交按钮事件 */
function submit() {
  props.updateMethod(form.value).then((res) => {
    proxy.$modal.msgSuccess("修改成功");
    submited();
  });
}

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
